﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PNTSystemClass.Framework.Window
{
    public partial class Window : Form
    {
        System.Diagnostics.Process ps = null;
        int index = 0;
        List<string> FileNames;
        public Window()
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        }

        private async void Ps_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
        {
            if (e.Data != null)
            {
                Logs.AppendText(e.Data);
            }

        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {
            
        }

        private void CodeAndShell_Panel1_SizeChanged(object sender, EventArgs e)
        {
            TabControl tc = (TabControl)CodeAndShell.Panel1.Controls[0];
            tc.Size = new Size(CodeAndShell.Panel1.Width - 3, CodeAndShell.Panel1.Height - 3);
            tc.Location = new Point(CodeAndShell.Panel1.Bounds.X, CodeAndShell.Panel1.Bounds.Y);
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "(*.pt)PNT代码文件|*.pt|(*.*)所有文件|*.*";
            dialog.DefaultExt = "pt";
            DialogResult dr = dialog.ShowDialog();
            if (dr == DialogResult.OK)
            {
                FileNames.Add(dialog.FileName);
                EditWindow.TabPages.Add(System.IO.Path.GetFileName(dialog.FileName));
                EditWindow.TabPages[index].Controls.Add(new RichTextBox());
                EditWindow.TabPages[index].Controls[0].Size = EditWindow.TabPages[0].Size;
                EditWindow.TabPages[index].Controls[0].Text = new System.IO.StreamReader(FileNames[index]).ReadToEndAsync().Result;
            }
            else
            {
                return;
            }
            index++;
        }

        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FileNames.Add("");
            EditWindow.TabPages.Add("新建代码页");
            EditWindow.TabPages[index].Controls.Add(new RichTextBox());
            EditWindow.TabPages[index].Controls[0].Size = EditWindow.TabPages[0].Size;
            index++;
        }

        string baocun()
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "PNT代码文件(*.pt)|*.pt|所有文件(*.*)|*.*";
            sfd.DefaultExt = "pt";
            DialogResult dr = sfd.ShowDialog();
            if (dr == DialogResult.OK)
            {
                FileNames[EditWindow.SelectedIndex] = sfd.FileName;
                EditWindow.SelectedTab.Text = System.IO.Path.GetFileName(sfd.FileName);
                System.IO.StreamWriter saveFile = new System.IO.StreamWriter(FileNames[EditWindow.SelectedIndex]);
                saveFile.Write(EditWindow.SelectedTab.Controls[0].Text);
                saveFile.Close();
                return sfd.FileName;
            }
            else
            {
                return "";
            }
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            baocun();
        }

        private void 解释ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string fn = baocun();
            ps.Kill();
            ps.Close();
            ps.StartInfo.Arguments = "comp:" + fn;
            ps.Start();
            string logs = ps.StandardOutput.ReadToEnd();
            Logs.Text = logs;
            ps.StartInfo.Arguments = "";
        }

        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBoxBase tbb = (TextBoxBase)EditWindow.SelectedTab.Controls[0];
            RichTextBox rtb = (RichTextBox)tbb;
            rtb.Copy();
        }

        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBoxBase tbb = (TextBoxBase)EditWindow.SelectedTab.Controls[0];
            RichTextBox rtb = (RichTextBox)tbb;
            rtb.Paste();
        }

        private void 工具ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBoxBase tbb = (TextBoxBase)EditWindow.SelectedTab.Controls[0];
            RichTextBox rtb = (RichTextBox)tbb;
            rtb.Cut();
        }

        private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBoxBase tbb = (TextBoxBase)EditWindow.SelectedTab.Controls[0];
            RichTextBox rtb = (RichTextBox)tbb;
            rtb.SelectAll();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBoxBase tbb = (TextBoxBase)EditWindow.SelectedTab.Controls[0];
            RichTextBox rtb = (RichTextBox)tbb;
            rtb.SelectedText.Replace(rtb.SelectedText, "");
        }

        private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBoxBase tbb = (TextBoxBase)EditWindow.SelectedTab.Controls[0];
            RichTextBox rtb = (RichTextBox)tbb;
            rtb.Undo();
        }

        private void 回退ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBoxBase tbb = (TextBoxBase)EditWindow.SelectedTab.Controls[0];
            RichTextBox rtb = (RichTextBox)tbb;
            rtb.Redo();
        }

        private void 修改颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBoxBase tbb = (TextBoxBase)EditWindow.SelectedTab.Controls[0];
            RichTextBox rtb = (RichTextBox)tbb;
            ColorDialog cd = new ColorDialog();
            cd.ShowDialog();
            rtb.SelectionColor = cd.Color;
        }

        private void 新建窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditWindow.TabPages.Add("新建窗口(不做文件操作)");
            EditWindow.TabPages[index].Controls.Add(new RichTextBox());
            EditWindow.TabPages[index].Controls[0].Size = EditWindow.TabPages[0].Size;
        }

        private void 窗口列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void Window_Load(object sender, EventArgs e)
        {
            ps = new System.Diagnostics.Process();
            ps.StartInfo.FileName = "PNTShellCore.exe";
            ps.StartInfo.UseShellExecute = false;
            ps.StartInfo.RedirectStandardOutput = true;
            ps.StartInfo.RedirectStandardError = true;
            ps.StartInfo.RedirectStandardInput = true;
            ps.StartInfo.CreateNoWindow = false;
            FileNames = new List<string>(10000);
            ps.OutputDataReceived += Ps_OutputDataReceived;
            ps.Start();
            ps.BeginOutputReadLine();
        }

        private void CommandTextB_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                ps.StandardInput.AutoFlush = true;
                ps.StandardInput.Write(CommandTextB.Text.Replace("\r\n",""));
                ps.StandardInput.Flush();
            }
        }

        private void Window_FormClosing(object sender, FormClosingEventArgs e)
        {
            ps.Kill();
            ps.Close();
            Application.ExitThread();
        }

        private void 文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}
